import 'package:flutter/material.dart';
import 'package:flutter_chip/utils/log_util.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class WebInfoPage extends StatefulWidget {
  final String title;
  final String url;
  final String type;

  WebInfoPage(this.title, this.url, {this.type});

  @override
  State<StatefulWidget> createState() {
    return _WebInfoPage();
  }
}

class _WebInfoPage extends State<WebInfoPage> {
  bool isLoading = false;
  FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
  String url, title;

  @override
  void initState() {
    super.initState();
    if (null == widget.type) {
      url = 'https://tdnr.gitee.io/limh/${widget.url}?height=0';
    } else {
      url = widget.url;
    }
    if (widget.title.isEmpty) {
      title = "详情";
    } else {
      title = widget.title;
    }
    LogUtil.v('${widget.title}:$url');
    flutterWebviewPlugin.onStateChanged.listen((wvs) {
      LogUtil.v(wvs.type);
      if (wvs.type == WebViewState.startLoad) {
        isLoading = true;
      } else if (wvs.type == WebViewState.finishLoad) {
        isLoading = false;
      }
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return WebviewScaffold(
      appBar: AppBar(
        elevation: 1,
        backgroundColor: Theme.of(context).canvasColor,
        title:
            Text(title, style: TextStyle(fontSize: 18, color: Colors.black87)),
      ),
      url: url,
      initialChild: Center(
        child: SpinKitWave(color: Theme.of(context).primaryColor, size: 30.0),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    flutterWebviewPlugin.dispose();
  }
}
